package com.skzh.system.service.impl;

import com.skzh.common.utils.DateUtils;
import com.skzh.common.utils.StringUtils;
import com.skzh.system.domain.BaseAttach;
import com.skzh.system.mapper.BaseAttachMapper;
import com.skzh.system.service.IBaseAttachService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 附件存储Service业务层处理
 *
 * @author wenlinchuan
 * @date 2024-06-20
 */
@Service
public class BaseAttachServiceImpl implements IBaseAttachService {
    @Autowired
    private BaseAttachMapper baseAttachMapper;

    /**
     * 查询附件存储
     *
     * @param attachId 附件存储主键
     * @return 附件存储
     */
    @Override
    public BaseAttach selectBaseAttachByAttachId(Long attachId) {
        return baseAttachMapper.selectBaseAttachByAttachId(attachId);
    }

    /**
     * 查询附件存储列表
     *
     * @param baseAttach 附件存储
     * @return 附件存储
     */
    @Override
    public List<BaseAttach> selectBaseAttachList(BaseAttach baseAttach) {
        return baseAttachMapper.selectBaseAttachList(baseAttach);
    }

    /**
     * 新增附件存储
     *
     * @param baseAttach 附件存储
     * @return 结果
     */
    @Override
    public int insertBaseAttach(BaseAttach baseAttach) {
        baseAttach.setCreateTime(DateUtils.getNowDate());
        return baseAttachMapper.insertBaseAttach(baseAttach);
    }

    /**
     * 修改附件存储
     *
     * @param baseAttach 附件存储
     * @return 结果
     */
    @Override
    public int updateBaseAttach(BaseAttach baseAttach) {
        baseAttach.setUpdateTime(DateUtils.getNowDate());
        return baseAttachMapper.updateBaseAttach(baseAttach);
    }

    /**
     * 批量删除附件存储
     *
     * @param attachIds 需要删除的附件存储主键
     * @return 结果
     */
    @Override
    public int deleteBaseAttachByAttachIds(Long[] attachIds) {
        return baseAttachMapper.deleteBaseAttachByAttachIds(attachIds);
    }

    /**
     * 删除附件存储信息
     *
     * @param attachId 附件存储主键
     * @return 结果
     */
    @Override
    public int deleteBaseAttachByAttachId(Long attachId) {
        return baseAttachMapper.deleteBaseAttachByAttachId(attachId);
    }

    /**
     * 更新附件，有则更新，没有新建
     *
     * @param list 附件list
     */
    @Override
    public void addOrUpdateAttachList(List<BaseAttach> list) {
        if (CollectionUtils.isEmpty(list)) {
            return;
        }
        // 存储需要删除的附件信息
        Map<String, Set<String>> deleteBizIdMap = new HashMap<>();
        list.forEach(baseAttach -> {
            // 判断是否有不合法数据
            if (StringUtils.isBlank(baseAttach.getAttachBizId())) {
                throw new RuntimeException("attach.id_null");
            }
            if (StringUtils.isBlank(baseAttach.getAttachBizTypeDict())) {
                throw new RuntimeException("attach.type_null");
            }
            deleteBizIdMap.computeIfAbsent(baseAttach.getAttachBizId(), k -> new HashSet<>());
            deleteBizIdMap.get(baseAttach.getAttachBizId()).add(baseAttach.getAttachBizTypeDict());
            baseAttachMapper.insertBaseAttach(baseAttach);
        });
        // 删除不需要的数据
        for (Map.Entry<String, Set<String>> entry : deleteBizIdMap.entrySet()) {
            for (String bizType : entry.getValue()) {
                baseAttachMapper.deleteByBizInfo(entry.getKey(), bizType);
            }
        }
    }

    /**
     * 根据业务ID、业务类型查询附件list
     *
     * @param bizId
     * @param bizType
     * @return attach list
     */
    @Override
    public List<BaseAttach> getAttachList(String bizId, String bizType) {
        if (StringUtils.isEmpty(bizId) || StringUtils.isEmpty(bizType)) {
            throw new RuntimeException("业务ID或者业务类型为空！");
        }
        return baseAttachMapper.getAttachListByBizInfo(bizId, bizType);
    }

    /**
     * 更新附件上传记录
     *
     * @param bizId        业务主键
     * @param bizType      附件类型
     * @param baseAttaches 附件列表
     */
    @Override
    public void updateBaseAttaches(String bizId, String bizType, List<BaseAttach> baseAttaches) {
        if (baseAttaches == null || baseAttaches.isEmpty()) {
            // 删除
            if (!StringUtils.isBlank(bizType)) {
                baseAttachMapper.deleteByBizInfo(bizId, bizType);
            }
        } else {
            // 全删
            baseAttachMapper.deleteByBizInfo(bizId, bizType);
            // 插入新上传附件的附件记录
            List<BaseAttach> dtos = baseAttaches.stream().filter(file -> file.getAttachId() == null).collect(Collectors.toList());
            if (!CollectionUtils.isEmpty(dtos)) {
                dtos.forEach(dto -> {
                    dto.setAttachBizId(bizId);
                    dto.setAttachBizTypeDict(bizType);
                    baseAttachMapper.insertBaseAttach(dto);
                });
            }
        }
    }

}
